package queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-07-16
 * Time: 15:33
 */
public class MyQueue {
   static class ListNode {
       public int val;
       public ListNode next;
       public ListNode prev;

       public ListNode(int val) {
           this.val = val;
       }

   }

   public ListNode first = null;
   public ListNode last = null;
   public int usedSize;

   public void offer (int val) {
       ListNode node = new ListNode(val);
       if (isEmpty()) {
           last = first = node;
       }else {
           //尾插
           last.next = node;
           node.prev = last;
           last = last.next;

       }
       this.usedSize++;
   }

   //删除前提下获取队列的队头
   public int poll() {
       if (isEmpty()){
           return -1;
       }

       int ret = first.val;
       first = first.next;

       //只删剩一个节点时，空没有prev！
       if(first != null) {
           first.prev = null;
       }
       this.usedSize--;
       return ret;
   }

   //不删除前提下获取队列的队头
   public int peek() {
       if (isEmpty()){
           return -1;
       }

       return first.val;
   }

   public boolean isEmpty() {
       return this.usedSize == 0;
//       return fast == null;
   }
}
